
require "crc"

module(..., package.seeall)

local logtag = "zz_sw"
local cid, uid = 3, 3 -- 默认用3通道
local ubuf, nbuf = {}, {} -- 串口和网络数据buffer
local dtu, udtu, udata = {}, {} -- 配置文件
local status,isLogin = 0,false
function setDtu(dtup, udtup, udatap)
    dtu, udtu, udata = dtup, udtup, udatap
    -- 如果设置是本协议
    if udtu and tonumber(udtu.ntype) == 11 then sys.taskInit(init) end
end
local function getLogin()
    -- login  @@40615131498IP:127.0.0.1##
    --        RX:+LANDSHA(服务器日期yymmddhhmmss)
    loginStr = "@@"..udtu.dsn.."IP:127.0.0.1##" -- 登录数据
    return loginStr
end
local function sendHB()
    sys.publish("NET_SENT_RDY_" .. (cid or 3), "HB") -- 使用登陆代替心跳
    status = 2
end
local function login()
    sys.publish("NET_SENT_RDY_" .. (cid or 3), getLogin()) 
    status = 0
end
function process(data, status)
    -- 68086830410405014002F09816 登陆成功
    if (status == 0 and data:sub(1, 8) == "+LANDSHA") then
        log.warn(logtag, "登陆成功")
        isLogin = true
    elseif (status == 1 and data:sub(1, 8) == "+LANDSHA") then
        log.warn(logtag, "上报数据成功")
    end
end
function valToBCD(val) -- 1548870->7088540100
    temp = tonumber(val)
    if not temp then return "0000000000" end
    temp = string.format("%010d",math.floor(temp)) 
    res = temp:sub(9,10)..temp:sub(7,8)..temp:sub(5,6)..temp:sub(3,4)..temp:sub(1,2)
    return res
end
function update()
    -- 等待读取数据
    isOK, data = sys.waitUntil("USRDATA_UPDATE", 20 * 1000)
    if isOK then
        dd = data
    else
        dd = usrdata.getVal()
    end
    --6818186801010101010180ce000a00 000000000e180b01180000000aad16
    if not dd or not dd.val or not dd.val[1] then
        log.error(logtag,"没有获取到设备数据，放弃上传")
        return
    end      
    line = tonumber(dd.val[1])
    temp = tonumber(dd.val[2] or 0)*1000
    csq = net.getRssi()
    vol = power.getVbat()
    crcData = "0" .. udtu.dsn .. "80CE".. vol .. csq .. line..temp
    dataHex = "68181868" .. crcData .. string.format("%02X",crc.crc8(crcData:fromHex())) .. "16"
    sys.publish("NET_SENT_RDY_" .. (cid or 3), dataHex:fromHex())
    status=1
end
sys.subscribe("DTU_ALARM_ON", function() sys.taskInit(update,false) end)--在实时在线模式时触发
function init()
    cid = (cid or 3) -- 网络通道id，对应uartid
    while true do
        isLogin = false
        while not socket.isReady() or not create.getDatalink(cid) do -- 
            log.warn(logtag, "等待net[" .. cid .. "] connect...")
            sys.wait(1000)
        end
        login()
        while create.getDatalink(cid) do
            -- 等待有网络下发数据，或者到指定的上报时间间隔
            result, id, data = sys.waitUntil("NET_RECV_WAIT_" .. cid, (tonumber(dtu.conf[cid][3]) or 180) * 1000)
            -- result, id, data = sys.waitUntil("NET_RECV_WAIT_" .. cid, 5 * 1000)
            -- 网络下发命令
            if result then
                if data then
                    log.warn(logtag, string.format("收到网络[%s]数据[status:%s]:%s", id, status, data:toHex()))
                    process(data, status)
                end
            else -- 超时(已到指定的时间间隔)
                log.warn(logtag, "发送心跳")
                sendHB()
            end
            if not isLogin then break end
        end
    end
end

